********************************************************************************
* DID TERRORISM AFFECT VOTING IN THE BREXIT REFERENDUM?
* BOVE, EFTHYVOULOU & PICKARD
* REPLICATION FILE
********************************************************************************

clear all
********************************************************************************

net install scheme-modern, from("https://raw.githubusercontent.com/mdroste/stata-scheme-modern/master/")
set scheme modern

// Globals
if "`c(username)'"=="harrypickard" {
glo project "/Users/harrypickard/Dropbox/Immigration, Terrorism & Voting/Terrorism & Voting/1_Final material/REPLICATION_MATERIAL"
}
glo data "$project/data"
glo output "$project/output"

glo controls "attack_hist01 quall4plus_sh_growth ManufacturingShareGrowth MigrantEUAccessionGrowth MigrantOldEUMemberGrowth MigrantElsewhereGrowth median_hourly_pay_growth muslim_sh_growth density ln_crime_total Total_EconomyEU_dependence"	

glo controls_pooled "male age age2 ed_med ed_high con15 lab15 ld15 snp15 pc15 ukip15 green15"


********************************************************************************
* DISTRICT-LEVEL ANALYSIS
********************************************************************************


// TABLE 1: Terrorism and the Remain vote
qui {
use "$data/d_data", clear

preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent if attack01 == 0, r clu(attack_cluster_cent)
restore

preserve
foreach x in $controls ukip_sup {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent ukip_sup if attack01 == 0, r clu(attack_cluster_cent) 
restore

preserve
foreach x in $controls TotalImpactFLWAAYR {
	ereplace `x' = std(`x') if main_sample_336 == 1
	}
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent TotalImpactFLWAAYR if attack01 == 0, r clu(attack_cluster_cent) 
restore

preserve
foreach x in $controls pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent pensionergrowth20012011 if attack01 == 0, r clu(attack_cluster_cent) 
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent pop if attack01 == 0, r clu(attack_cluster_cent) 
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent twitter_75 if attack01 == 0, r clu(attack_cluster_cent) 
restore

preserve
foreach x in $controls ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}
eststo: reg remain $controls attack_hist01 i.attack_cluster_cent dist_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if attack01 == 0, r clu(attack_cluster_cent) 
restore

	esttab est* using "$output/table_1", star(* .1 ** .05 *** .01) p b(3) r2 ///
	keep(dist_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75) ///
	coeflabel(dist_cent "Distance" ukip_sup "UKIP support" TotalImpactFLWAAYR "Austerity shock" ///
	pensionergrowth20012011 "Pensioner share growth" pop "Population" twitter_75 "Twitter usage (75th percentile)") ///
	s(r2 N, label("R-squared" "Observations")) ///
	replace csv nonotes
	est clear	
}


// TABLE 2: Terrorism and turnout
qui {
use "$data/d_data", clear

preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent if attack01 == 0, r clu(attack_cluster_cent)
restore

preserve
foreach x in $controls ukip_sup {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent ukip_sup if attack01 == 0, r clu(attack_cluster_cent) 
restore

preserve
foreach x in $controls TotalImpactFLWAAYR {
	ereplace `x' = std(`x') if main_sample_336 == 1
	}
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent TotalImpactFLWAAYR if attack01 == 0, r clu(attack_cluster_cent) 
restore

preserve
foreach x in $controls pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent pensionergrowth20012011 if attack01 == 0, r clu(attack_cluster_cent) 
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent pop if attack01 == 0, r clu(attack_cluster_cent) 
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent twitter_75 if attack01 == 0, r clu(attack_cluster_cent) 
restore

preserve
foreach x in $controls ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}
eststo: reg turnout $controls attack_hist01 i.attack_cluster_cent dist_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if attack01 == 0, r clu(attack_cluster_cent) 
su $controls attack_hist01 ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if e(sample) == 1
restore

	esttab est* using "$output/table_2", star(* .1 ** .05 *** .01) p b(3) r2 ///
	keep(dist_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75) ///
	coeflabel(dist_cent "Distance" ukip_sup "UKIP support" TotalImpactFLWAAYR "Austerity shock" ///
	pensionergrowth20012011 "Pensioner share growth" pop "Population" twitter_75 "Twitter usage (75th percentile)") ///
	s(r2 N, label("R-squared" "Observations")) ///
	replace csv nonotes
	est clear	
}


// TABLE 3: Terrorism, the Remain vote and turnout: IV estimates
qui {
use "$data/d_data", clear


preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: ivreg2 remain (dist_cent = dist_hist7079) $controls attack_hist01 i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent) first
		estadd scalar fstat = e(widstat), replace
		estadd scalar ar_p = e(archi2p), replace
restore		
		
preserve
foreach x in $controls ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}		
eststo: ivreg2 remain (dist_cent = dist_hist7079) $controls attack_hist01 i.attack_cluster_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if attack01 == 0, r clu(attack_cluster_cent) first
		estadd scalar fstat = e(widstat), replace
		estadd scalar ar_p = e(archi2p), replace
restore

preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: ivreg2 turnout (dist_cent = dist_hist7079) $controls attack_hist01 i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent) first
		estadd scalar fstat = e(widstat), replace
		estadd scalar ar_p = e(archi2p), replace
restore		
		
preserve
foreach x in $controls ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}		
eststo: ivreg2 turnout (dist_cent = dist_hist7079) $controls attack_hist01 i.attack_cluster_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if attack01 == 0, r clu(attack_cluster_cent) first
		estadd scalar fstat = e(widstat), replace
		estadd scalar ar_p = e(archi2p), replace
restore

		esttab est* ///
		using "$output/table_3", star(* .1 ** .05 *** .01) b(3) p ///
		stats(fstat ar_p N, label("Excluded F-stat" "AR chi-squared test (p)" "Observations")) ///
		keep(dist_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75) ///
		order(dist_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75) ///
		coeflabel(dist_cent "Distance" ukip_sup "UKIP support" TotalImpactFLWAAYR "Austerity shock" ///
		pensionergrowth20012011 "Pensioner share growth" pop "Population" twitter_75 "Twitter usage (75th percentile)") ///	
		replace csv nonotes
		est clear
		
	
preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: reg dist_cent dist_hist7079 $controls attack_hist01 i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent)	
restore

preserve
foreach x in $controls ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}		
eststo: reg dist_cent dist_hist7079 $controls attack_hist01 i.attack_cluster_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if attack01 == 0, r clu(attack_cluster_cent)	
restore

preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
eststo: reg dist_cent dist_hist7079 $controls attack_hist01 i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent)
restore

preserve
foreach x in $controls ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop {
	ereplace `x' = std(`x') if main_sample_335 == 1
	}		
eststo: reg dist_cent dist_hist7079 $controls attack_hist01 i.attack_cluster_cent ukip_sup TotalImpactFLWAAYR pensionergrowth20012011 pop twitter_75 if attack01 == 0, r clu(attack_cluster_cent)	
restore

		esttab est* ///
		using "$output/table_3", star(* .1 ** .05 *** .01) b(3) p ///
		stats(fstat N, label("Excluded F-stat" "Observations")) ///
		keep(dist_hist7079) ///
		coeflabel(dist_hist7079 "Distance (1970-1979)") ///	
		append csv nonotes
		est clear		
}		


// FIGURE 1: Heterogeneous effects
qui {
use "$data/d_data", clear


qui {
preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
reg remain $controls attack_hist01 c.dist_cent##i.hits_a20 i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent)
margins, dydx(dist_cent) over(hits_a20) level(95)
marginsplot, yline(0, lpattern(solid) lcolor(red)) ///
xlabel(-0.5 " " 0 1 1.5 " ", notick labsize(3.5)) ///
ylabel(0 -0.02 -0.04 -0.06 -0.08 -0.1, labsize(3)) ///
plot1opts(lcolor(black) mcolor(black)) ///
recast(scatter) ///
ciopts(lpattern(solid) lcolor(black)) ///
scheme(modern) ///
ytitle("", size(medium)) ///
xtitle ("High media coverage", size(medium)) ///
legend(off) title("Panel (a)", nospan size(medium) color(black)) ///
saving("$output/a", replace) aspect(2)
restore
}

qui {
preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
reg remain $controls attack_hist01 c.dist_cent##i.islam_terror i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent)
margins, dydx(dist_cent) over(islam_terror) level(95)
marginsplot, yline(0, lpattern(solid) lcolor(red)) ///
xlabel(-0.5 " " 0 1 1.5 " ", notick labsize(3.5)) ///
ylabel(0 -0.02 -0.04 -0.06 -0.08 -0.1, labsize(3)) ///
plot1opts(lcolor(black) mcolor(black)) ///
recast(scatter) ///
ciopts(lpattern(solid) lcolor(black)) ///
scheme(modern) ///
ytitle("", size(medium)) ///
xtitle ("Muslim/Jihadi perpetrators", size(medium)) ///
legend(off) title("Panel (b)", nospan size(medium) color(black)) ///
saving("$output/b", replace) aspect(2)
restore
}

qui {
preserve
foreach x in $controls {
	ereplace `x' = std(`x') if main_sample_337 == 1
	}
reg remain $controls attack_hist01 c.dist_cent##i.kill i.attack_cluster_cent if attack01 == 0, r clu(attack_cluster_cent)
margins, dydx(dist_cent) over(kill) level(95)
marginsplot, yline(0, lpattern(solid) lcolor(red)) ///
xlabel(-0.5 " " 0 1 1.5 " ", notick labsize(3.5)) ///
ylabel(0 -0.02 -0.04 -0.06 -0.08 -0.1, labsize(3)) ///
plot1opts(lcolor(black) mcolor(black)) ///
recast(scatter) ///
ciopts(lpattern(solid) lcolor(black)) ///
scheme(modern) ///
ytitle("", size(medium)) ///
xtitle ("Fatal outcomes", size(medium)) ///
legend(off) title("Panel (c)", nospan size(medium) color(black)) ///
saving("$output/c", replace) aspect(2)
restore
}


gr combine "$output/a" "$output/b" "$output/c", ///
xcommon ycommon rows(1) ///
b1("") l1("Marginal Effects of Distance")
graph export "$output/figure_1.pdf", as(pdf) replace
erase "$output/a.gph"
erase "$output/b.gph"
erase "$output/c.gph"

}




********************************************************************************
* INDIVIDUAL-LEVEL ANALYSIS
********************************************************************************


// TABLE 4: Terrorism and pro-EU sentiment: individual-level analysis
qui {	
use "$data/i_data", clear

	** All individuals
eststo: xi: reg EUIntegrationSelf post_attack, r cluster(lad16cd)
	estadd local lad "No", replace
	estadd local county "No", replace
	estadd local gor "No", replace
	estadd local controls "No", replace
eststo: xi: reg EUIntegrationSelf post_attack i.gor_code*i.wave, r cluster(lad16cd)
	estadd local gor "Yes", replace
	estadd local controls "No", replace
eststo: xi: reg EUIntegrationSelf post_attack i.gor_code*i.wave $controls_pooled, r cluster(lad16cd)
	estadd local gor "Yes", replace
	estadd local controls "Yes", replace	
eststo: xi: reg EUIntegrationSelf post_attack i.nuts218cd*i.wave, r cluster(lad16cd)
	estadd local county "Yes", replace
	estadd local controls "No", replace
eststo: xi: reg EUIntegrationSelf post_attack i.nuts218cd*i.wave $controls_pooled, r cluster(lad16cd)
	estadd local county "Yes", replace
	estadd local controls "Yes", replace			
eststo: xi: reg EUIntegrationSelf post_attack i.lad16cd*i.wave, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "No", replace
eststo: xi: reg EUIntegrationSelf post_attack i.lad16cd*i.wave $controls_pooled, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "Yes", replace
	
	* Comparing within attacked counties
eststo: reg EUIntegrationSelf post_attack if attack_lad_county == 1, r cluster(lad16cd)
	estadd local lad "No", replace
	estadd local controls "No", replace
eststo: xi: reg EUIntegrationSelf post_attack i.lad16cd*i.wave if attack_lad_county == 1, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "No", replace
eststo: xi: reg EUIntegrationSelf post_attack $controls_pooled i.lad16cd*i.wave if attack_lad_county == 1, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "Yes", replace		

	esttab est* using "$output/table_4", star(* .1 ** .05 *** .01) p b(3) r2 ///
	keep(post_attack) ///
	coeflabel(post_attack "Post-attack") ///
	s(gor county lad controls r2 N, label("GOR-by-survey FEs" "County-by-survey FEs" "LAD-by-survey FEs" "Vector Z" "R-squared" "Observations")) ///
	replace csv nonotes
	est clear
}		


// TABLE 5: Perceptions of terrorism outside the EU: individual-level analysis
qui {		
use "$data/i_data", clear
drop if wave == 12
	
eststo: reg effectsEUTerror01 post_attack, r cluster(lad16cd)

eststo: xi: reg effectsEUTerror01 post_attack i.gor_code*i.wave, r cluster(lad16cd)
	estadd local gor "Yes", replace
eststo: xi: reg effectsEUTerror01 post_attack i.gor_code*i.wave $controls_pooled, r cluster(lad16cd)
	estadd local gor "Yes", replace
	estadd local controls "Yes", replace
eststo: xi: reg effectsEUTerror01 post_attack i.nuts218cd*i.wave, r cluster(lad16cd)
	estadd local county "Yes", replace
eststo: xi: reg effectsEUTerror01 post_attack i.nuts218cd*i.wave $controls_pooled, r cluster(lad16cd)
	estadd local county "Yes", replace
	estadd local controls "Yes", replace
eststo: xi: reg effectsEUTerror01 post_attack i.lad16cd*i.wave, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "No", replace
eststo: xi: reg effectsEUTerror01 post_attack $controls_pooled i.lad16cd*i.wave, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "Yes", replace
	
	* Within attacked counties
eststo: reg effectsEUTerror01 post_attack if attack_lad_county == 1, r cluster(lad16cd)
eststo: xi: reg effectsEUTerror01 post_attack i.lad16cd*i.wave if attack_lad_county == 1, r cluster(lad16cd)
	estadd local lad "Yes", replace
eststo: xi: reg effectsEUTerror01 post_attack $controls_pooled i.lad16cd*i.wave if attack_lad_county == 1, r cluster(lad16cd)
	estadd local lad "Yes", replace
	estadd local controls "Yes", replace

	
	esttab est* using "$output/table_5", star(* .1 ** .05 *** .01) p b(3) r2 ///
	keep(post_attack) ///
	coeflabel(post_attack "Post-attack") ///
	s(gor county lad controls r2 N, label("GOR-by-survey FEs" "County-by-survey FEs" "LAD-by-survey FEs" "Vector Z" "R-squared" "Observations")) ///
	replace csv nonotes
	est clear
}		


	
clear all	
